package com.melesta.payment.inapp3;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import com.google.Consts.ResponseCode;
import com.melesta.engine.DatabaseHelper;
import com.melesta.engine.DatabaseHelper.IObjectFilter;
import com.melesta.engine.EngineActivity;
import com.melesta.engine.EngineActivity.ActivityTickListener;
import com.melesta.engine.Error;
import com.melesta.payment.PaymentManager;
import com.melesta.payment.PaymentTransaction;
import com.melesta.payment.interfaces.IListener;
import com.melesta.payment.interfaces.IRequest;
import com.melesta.payment.interfaces.IRequestBuilder;
import com.melesta.payment.interfaces.ISystem;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class InAppSystem
  implements ISystem, EngineActivity.ActivityTickListener
{
  static final int RC_REQUEST = 10001;
  static final String SYSTEM_NAME = "inapp";
  private static final String TAG = "payment-inapp";
  private String base64EncodedPublicKey;
  private IListener defaultListener;
  private final ArrayList<ConsumeData> mConsumeRequests = new ArrayList();
  private Context mContext;
  IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener()
  {
    public void onQueryInventoryFinished(IabResult paramAnonymousIabResult, final Inventory paramAnonymousInventory)
    {
      Log.d("payment-inapp", "Query inventory finished.");
      if (paramAnonymousIabResult.isFailure())
      {
        Log.d("payment-inapp", "Failed to query inventory: " + paramAnonymousIabResult);
        return;
      }
      EngineActivity.getInstance().runOnGLThread(new Runnable()
      {
        public void run()
        {
          Iterator localIterator = paramAnonymousInventory.getAllSkuDetails().iterator();
          while (localIterator.hasNext())
          {
            SkuDetails localSkuDetails = (SkuDetails)localIterator.next();
            String str = PaymentManager.getInstance().getRequestNameBySku(InAppSystem.this.getName(), localSkuDetails.getSku());
            InAppSystem.this.defaultListener.updateSkuDetails(str, localSkuDetails.getPrice(), localSkuDetails.getTitle());
          }
        }
      });
      Log.d("payment-inapp", "Query inventory was successful.");
      DatabaseHelper localDatabaseHelper = DatabaseHelper.getHelper(InAppSystem.this.mContext);
      final String str = InAppSystem.this.getName();
      List localList = localDatabaseHelper.getTransactions(new DatabaseHelper.IObjectFilter()
      {
        public boolean filter(Object paramAnonymous2Object)
        {
          PaymentTransaction localPaymentTransaction = (PaymentTransaction)paramAnonymous2Object;
          return (str.equals(localPaymentTransaction.paymentSystem)) && (localPaymentTransaction.consumeTime == -1L);
        }
      });
      PaymentTransaction.dump(localList);
      Iterator localIterator1 = paramAnonymousInventory.getAllPurchases().iterator();
      label118: Purchase localPurchase;
      Object localObject;
      if (localIterator1.hasNext())
      {
        localPurchase = (Purchase)localIterator1.next();
        Iterator localIterator2 = localList.iterator();
        PaymentTransaction localPaymentTransaction;
        do
        {
          boolean bool = localIterator2.hasNext();
          localObject = null;
          if (!bool)
            break;
          localPaymentTransaction = (PaymentTransaction)localIterator2.next();
        }
        while ((!str.equals(localPaymentTransaction.paymentSystem)) || (!localPaymentTransaction.paymentId.equals(localPurchase.mDeveloperPayload)) || (localPaymentTransaction.consumeTime != -1L));
        localObject = localPaymentTransaction;
        if (localObject != null)
          break label322;
        localObject = new PaymentTransaction();
        ((PaymentTransaction)localObject).paymentId = localPurchase.mSku;
        ((PaymentTransaction)localObject).paymentSystem = InAppSystem.this.getName();
        ((PaymentTransaction)localObject).quantity = 1;
        ((PaymentTransaction)localObject).time = System.currentTimeMillis();
        ((PaymentTransaction)localObject).customData = localPurchase.mOriginalJson;
        localDatabaseHelper.saveTransaction((PaymentTransaction)localObject);
      }
      while (true)
      {
        InAppSystem.ConsumeFinishedListener localConsumeFinishedListener = new InAppSystem.ConsumeFinishedListener(InAppSystem.this, (PaymentTransaction)localObject, localDatabaseHelper);
        InAppSystem.this.consume(localPurchase, localConsumeFinishedListener);
        break label118;
        break;
        label322: if (((PaymentTransaction)localObject).time == -1L)
        {
          ((PaymentTransaction)localObject).time = System.currentTimeMillis();
          ((PaymentTransaction)localObject).customData = localPurchase.mOriginalJson;
          localDatabaseHelper.saveTransaction((PaymentTransaction)localObject);
        }
      }
    }
  };
  private Handler mHandler;
  IabHelper mHelper;

  public void buy(InAppRequest paramInAppRequest)
  {
    final String str1 = paramInAppRequest.getName();
    final String str2 = paramInAppRequest.getSku();
    final int i = paramInAppRequest.getCountPerItem();
    Log.d("payment-inapp", "buying: " + str1 + " sku: " + str2);
    final PaymentTransaction localPaymentTransaction = new PaymentTransaction();
    localPaymentTransaction.paymentId = paramInAppRequest.getName();
    localPaymentTransaction.paymentSystem = getName();
    localPaymentTransaction.quantity = paramInAppRequest.getCountPerItem();
    final DatabaseHelper localDatabaseHelper = DatabaseHelper.getHelper(EngineActivity.getInstance());
    localDatabaseHelper.saveTransaction(-1L, localPaymentTransaction);
    IabHelper.OnIabPurchaseFinishedListener local3 = new IabHelper.OnIabPurchaseFinishedListener()
    {
      public void onIabPurchaseFinished(IabResult paramAnonymousIabResult, Purchase paramAnonymousPurchase)
      {
        PaymentManager localPaymentManager = PaymentManager.getInstance();
        Log.d("payment-inapp", "Purchase finished: " + paramAnonymousIabResult + ", purchase: " + paramAnonymousPurchase);
        PaymentTransaction.dump(localDatabaseHelper.getTransactions());
        InAppResponse localInAppResponse = new InAppResponse().setName(str1).setSku(str2).setCountPerItem(i).setListener(InAppSystem.this.defaultListener);
        InAppRequest localInAppRequest = (InAppRequest)localPaymentManager.getActiveRequest(new InAppRequestFilter().setSku(str2));
        if (localInAppRequest == null)
          return;
        if (paramAnonymousIabResult.isFailure())
        {
          localDatabaseHelper.deleteTransaction(localPaymentTransaction.internalId);
          Log.d("payment-inapp", "Purchase failed.");
          localInAppResponse.setStatus(InAppResponse.Status.ERROR).setErrorCode(Consts.ResponseCode.RESULT_ERROR.ordinal());
        }
        while (true)
        {
          localPaymentManager.postResponse(localInAppResponse);
          localPaymentManager.removeActiveRequest(localInAppRequest);
          return;
          if (paramAnonymousIabResult.isCanceled())
          {
            localDatabaseHelper.deleteTransaction(localPaymentTransaction.internalId);
            Log.d("payment-inapp", "Purchase failed.");
            localInAppResponse.setStatus(InAppResponse.Status.CANCEL).setErrorCode(Consts.ResponseCode.RESULT_USER_CANCELED.ordinal());
          }
          else
          {
            localPaymentTransaction.time = System.currentTimeMillis();
            localPaymentTransaction.customData = paramAnonymousPurchase.mOriginalJson;
            long l = localDatabaseHelper.saveTransaction(localPaymentTransaction);
            localInAppResponse.setStatus(InAppResponse.Status.PURCHASED).setInternalId(l);
            InAppSystem.this.mHelper.consumeAsync(paramAnonymousPurchase, new InAppSystem.ConsumeFinishedListener(InAppSystem.this, localPaymentTransaction, localDatabaseHelper));
            Log.d("payment-inapp", "Purchase successful.");
          }
        }
      }
    };
    this.mHelper.launchPurchaseFlow((Activity)this.mContext, str2, 10001, local3, paramInAppRequest.getName());
  }

  void consume(final ConsumeData paramConsumeData)
  {
    this.mHandler.post(new Runnable()
    {
      public void run()
      {
        try
        {
          InAppSystem.this.mConsumeRequests.add(paramConsumeData);
          EngineActivity.getInstance().addTickListener(InAppSystem.this);
          return;
        }
        catch (Exception localException)
        {
          Error.processException(localException);
        }
      }
    });
  }

  void consume(Purchase paramPurchase, ConsumeFinishedListener paramConsumeFinishedListener)
  {
    consume(new ConsumeData(paramPurchase, paramConsumeFinishedListener));
  }

  public Object get(String paramString)
  {
    return null;
  }

  public IListener getDefaultListener()
  {
    return this.defaultListener;
  }

  public String getName()
  {
    return "inapp";
  }

  public IRequestBuilder getRequestBuilder()
  {
    return new InAppRequestBuilder(this);
  }

  public boolean onActivityResult(Context paramContext, int paramInt1, int paramInt2, Intent paramIntent)
  {
    return this.mHelper.handleActivityResult(paramInt1, paramInt2, paramIntent);
  }

  public void onBeginTick()
  {
    final EngineActivity localEngineActivity;
    try
    {
      if (this.mHelper == null)
        return;
      if (this.mHelper.mAsyncInProgress)
        return;
      localEngineActivity = EngineActivity.getInstance();
      if (this.mConsumeRequests.isEmpty())
      {
        localEngineActivity.getHandler().post(new Runnable()
        {
          public void run()
          {
            localEngineActivity.removeTickListener(InAppSystem.this);
          }
        });
        return;
      }
    }
    catch (Exception localException)
    {
      Error.processException(localException);
      return;
    }
    final ConsumeData localConsumeData = (ConsumeData)this.mConsumeRequests.remove(0);
    localEngineActivity.getHandler().post(new Runnable()
    {
      public void run()
      {
        InAppSystem.this.mHelper.consumeAsync(localConsumeData.mPurchase, localConsumeData.mListener);
      }
    });
  }

  public void onCreate(Context paramContext, Handler paramHandler)
  {
    this.mHandler = paramHandler;
    this.mContext = paramContext;
    Log.d("payment-inapp", "Creating IAB helper.");
    this.mHelper = new IabHelper(paramContext, this.base64EncodedPublicKey);
    this.mHelper.enableDebugLogging(true);
    Log.d("payment-inapp", "Starting setup.");
    this.mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
    {
      public void onIabSetupFinished(IabResult paramAnonymousIabResult)
      {
        Log.d("payment-inapp", "Setup finished.");
        if (paramAnonymousIabResult.isFailure())
        {
          Log.d("payment-inapp", "Problem setting up in-app billing: " + paramAnonymousIabResult);
          return;
        }
        Log.d("payment-inapp", "Setup successful. Querying inventory.");
        List localList = PaymentManager.getInstance().getAllRequests(InAppSystem.this.getName());
        ArrayList localArrayList = new ArrayList();
        Iterator localIterator = localList.iterator();
        while (localIterator.hasNext())
          localArrayList.add(((IRequest)localIterator.next()).getSku());
        InAppSystem.this.mHelper.queryInventoryAsync(true, localArrayList, InAppSystem.this.mGotInventoryListener);
      }
    });
  }

  public void onDestroy(Context paramContext, Handler paramHandler)
  {
  }

  public void onEndTick()
  {
  }

  public void onPause(Context paramContext, Handler paramHandler)
  {
  }

  public void onResume(Context paramContext, Handler paramHandler)
  {
  }

  public void onStart(Context paramContext, Handler paramHandler)
  {
  }

  public void onStop(Context paramContext, Handler paramHandler)
  {
  }

  public void purchaseProcessed(PaymentTransaction paramPaymentTransaction)
  {
    if (paramPaymentTransaction != null)
      try
      {
        if (!paramPaymentTransaction.paymentSystem.equals(getName()))
          return;
        DatabaseHelper localDatabaseHelper = DatabaseHelper.getHelper(this.mContext);
        consume(new ConsumeData(new Purchase("inapp", paramPaymentTransaction.customData, ""), new ConsumeFinishedListener(paramPaymentTransaction, localDatabaseHelper)));
        return;
      }
      catch (Exception localException1)
      {
        StringBuilder localStringBuilder = new StringBuilder(128);
        try
        {
          PaymentTransaction.dump(paramPaymentTransaction, localStringBuilder);
          label84: Error.processException(localStringBuilder.toString(), localException1);
          return;
        }
        catch (Exception localException2)
        {
          break label84;
        }
      }
  }

  public InAppSystem set(String paramString, Object paramObject)
  {
    if ("openkey".equals(paramString))
      this.base64EncodedPublicKey = paramObject.toString();
    return this;
  }

  public InAppSystem setDefaultListener(IListener paramIListener)
  {
    this.defaultListener = paramIListener;
    return this;
  }

  private class ConsumeData
  {
    InAppSystem.ConsumeFinishedListener mListener;
    Purchase mPurchase;

    ConsumeData(Purchase paramConsumeFinishedListener, InAppSystem.ConsumeFinishedListener arg3)
    {
      this.mPurchase = paramConsumeFinishedListener;
      Object localObject;
      this.mListener = localObject;
    }
  }

  private class ConsumeFinishedListener
    implements IabHelper.OnConsumeFinishedListener
  {
    private final DatabaseHelper mDdHelper;
    private final PaymentTransaction mTransaction;

    public ConsumeFinishedListener(PaymentTransaction paramDatabaseHelper, DatabaseHelper arg3)
    {
      this.mTransaction = paramDatabaseHelper;
      Object localObject;
      this.mDdHelper = localObject;
    }

    public void onConsumeFinished(Purchase paramPurchase, IabResult paramIabResult)
    {
      Log.d("payment-inapp", "consumed");
      this.mTransaction.consumeTime = System.currentTimeMillis();
      this.mDdHelper.saveTransaction(this.mTransaction);
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.melesta.payment.inapp3.InAppSystem
 * JD-Core Version:    0.6.2
 */